package com.speedchina.common.message.service.impl;
import com.speedchina.common.message.entity.Alert;
import com.speedchina.common.message.entity.SendStyle;
import com.speedchina.common.message.email.service.MailService;
import com.speedchina.common.message.service.AlertService;
import com.speedchina.common.message.util.SendResult;
import com.speedchina.common.message.wechat.message.TextMessage;
import com.speedchina.common.message.wechat.service.WechatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
/**
 * @author zyw
 */
@Service
public class AlertServiceImpl implements AlertService {
    @Value("${speedchina.message.sendStyle:}")
    String sendStyle;
    @Autowired
    WechatService wechatService;
    @Autowired
    MailService mailService;
    public void sendAlert(Alert alert) {
        // todo 后续放入rabbitmq中进行消息处理
        String[] sendStyles = sendStyle.split(",");
        for (int i = 0; i < sendStyles.length; i++) {
            if (String.valueOf(SendStyle.WECHAT).equals(sendStyles[i])) {
                this.wechatSend(alert);
            } else if (String.valueOf(SendStyle.EMAIL).equals(sendStyles[i])) {
                mailService.sendSimpleMail(alert.getTitle()==null?"异常报警":alert.getTitle(), alert.getContent());
            }
        }
    }
    public void sendAlert(String title,String content) {
        Alert alert = new Alert();
        alert.setTitle(title);
        alert.setContent(content);
        this.sendAlert(alert);
    }
    public SendResult wechatSend(Alert alert){
        TextMessage textMessage = new TextMessage();
        textMessage.setText(alert.getContent());
        textMessage.setAtAll(true);
        SendResult send = wechatService.send(textMessage);
        return send;
    }
}
